package com.tms.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 雇员数据源Mybatis配置类
 */
@Configuration
@MapperScan(value = "com.tms.mapper.staff", sqlSessionFactoryRef = "sqlSessionFactoryBeanStaff")
public class MybatisConfigStaff {

    @Autowired
    @Qualifier("dsStaff")
    DataSource dsStaff;

    @Bean
    SqlSessionFactory sqlSessionFactoryBeanStaff() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dsStaff);
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mappers/staff/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    SqlSessionTemplate sqlSessionTemplateStaff() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBeanStaff());
    }
}
